<?php

namespace addon\lian\shop\controller;

use addon\lian\model\LianReward;
use app\shop\controller\BaseShop;
use think\facade\Log;

/**
 * 链动奖励记录控制器
 */
class Reward extends BaseShop
{
    /**
     * 链动奖励记录列表
     */
    public function lists()
    {
        // 更精确地判断是否为AJAX请求
        if (request()->isAjax() && !request()->isGet()) {
            try {
                $model = new LianReward();
                $page = input('page', 1);
                $page_size = input('page_size', PAGE_LIST_ROWS);
                $condition = [
                    ['site_id', '=', $this->site_id]
                ];
                
                // 搜索条件
                $member_search = input('member_search', '');
                $level_id = input('level_id', '');
                $reward_type = input('reward_type', '');
                $thaw_status = input('thaw_status', '');
                $start_time = input('start_time', '');
                $end_time = input('end_time', '');
                
                // 会员昵称/手机号搜索
                if ($member_search) {
                    $condition[] = ['m.member_name|m.mobile', 'like', '%' . $member_search . '%'];
                }
                
                // 链动等级
                if ($level_id !== '') {
                    $condition[] = ['level_id', '=', $level_id];
                }
                
                // 奖励类型
                if ($reward_type !== '') {
                    $condition[] = ['reward_type', '=', $reward_type];
                }
                
                // 解冻状态
                if ($thaw_status !== '') {
                    $condition[] = ['thaw_status', '=', $thaw_status];
                }
                
                // 时间范围
                if ($start_time && $end_time) {
                    $condition[] = ['create_time', 'between', [strtotime($start_time), strtotime($end_time)]];
                } elseif ($start_time) {
                    $condition[] = ['create_time', '>=', strtotime($start_time)];
                } elseif ($end_time) {
                    $condition[] = ['create_time', '<=', strtotime($end_time)];
                }
                
                $result = $model->getPage($this->site_id, $condition, $page, $page_size);

                return $result;
            } catch (\Exception $e) {
                Log::error('Reward lists exception: ' . $e->getMessage());
            }
        } else {
            // 奖励类型
            $reward_type_list = [
                0 => '直推奖',
                1 => '见点奖',
                2 => '培育奖',
                3 => '平级奖',
                4 => '复购见单奖',
                9 => '解冻结算'
            ];
            $this->assign('reward_type_list', $reward_type_list);
            
            // 解冻状态
            $thaw_status_list = [
                -1 => '失效',
                0 => '待解冻',
                1 => '已解冻'
            ];
            $this->assign('thaw_status_list', $thaw_status_list);
            
            // 获取链动等级列表
            $level_model = new \addon\lian\model\LianLevel();
            $level_list = $level_model->getLevelList([['site_id', '=', $this->site_id]], 'level_id, level_name');
            $this->assign('level_list', $level_list['data']);
            
            return $this->fetch('reward/lists');
        }
    }
    
    /**
     * 奖励记录详情
     */
    public function detail()
    {
        $id = input('id', 0);
        if (empty($id)) {
            $this->error('参数错误');
        }
        
        $model = new LianReward();
        $info = $model->getRewardDetail($this->site_id, $id);
        
        if (empty($info['data'])) {
            $this->error('奖励记录不存在');
        }
        
        $this->assign('info', $info['data']);
        
        // 奖励类型
        $reward_type_list = [
            0 => '直推奖',
            1 => '见点奖',
            2 => '培育奖',
            3 => '平级奖',
            4 => '复购见单奖',
            9 => '解冻结算'
        ];
        $this->assign('reward_type_name', $reward_type_list[$info['data']['reward_type']] ?? '未知');
        
        // 解冻状态
        $thaw_status_list = [
            -1 => '失效',
            0 => '待解冻',
            1 => '已解冻'
        ];
        $this->assign('thaw_status_name', $thaw_status_list[$info['data']['thaw_status']] ?? '未知');
        
        return $this->fetch('reward/detail');
    }


    public function del($id){
        $model = new LianReward();
        $result = $model->del($this->site_id, $id);
        return $result;
    }
}